package com.example.bee_work_3.fragment.project;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.bee_work_3.webc_ontent.IntentCallBack;
import com.example.bee_work_3.fragment.ListItem;
import com.example.bee_work_3.R;

import java.util.List;

public class ProjectDataAdapter extends ArrayAdapter<ListItem> {
    private int mResource;
    private Context mContext;
    private List<ListItem> mList;
    private IntentCallBack mCallBack;

    public ProjectDataAdapter(Context context, int resource, List<ListItem> list, IntentCallBack callBack) {
        super(context, resource);
        this.mList = list;
        this.mContext = context;
        this.mResource = resource;
        this.mCallBack = callBack;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView!=null){
            return convertView;
        }
        View view = LayoutInflater.from(mContext).inflate(mResource, null);
        final ListItem item = mList.get(position);
        TextView title = view.findViewById(R.id.text);
        ImageView imageView = view.findViewById(R.id.image);
        CardView cardView = view.findViewById(R.id.project_data_card);
        title.setText(item.getText());
        if (item.getImage() != null) {
            final int[] width = {100};
            final int[] height = {100};
            Glide.with(mContext)
                    .load(item.getImage())
                    .override(480, 800)
                    .into(imageView);
        }
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onIntent(item);
            }
        });
        return view;
    }
}
